import { dateFormat, dayDiff } from '@/utils/formatDate.js'
const dayjs = require('@/utils/dayjs.js')

export default {
  data() {
    return {
      info: null, // 询价单信息
      biddingList: [], // 报价列表
      remainder: '', //剩余时间
      bidText: '暂无报价信息', // 暂无报价
      showModal: false, //弹窗
      modalText: '',
      modalBtnLeft: '取消',
      modalBtnRight: '确定上架',
      modalTitle: '取消询价',
      eventType: '',
      id: '',
      resetDate: '',
      pageLoading: false, //初始化加载效果
    }
  },

  computed: {
    statusIcon() {
      return this.$store.state.accessory.statusIcon
    },
    status() {
      return this.$store.state.accessory.status
    },
    quality() {
      return this.$store.state.accessory.quality
    },
    formatName() {
      if (this.info.trueName) {
        return (
          this.info.trueName.slice(0, 1) +
          '*'.repeat(this.info.trueName.length - 1)
        )
      }
      return ''
    },
    formatPhone() {
      if (this.info.store_Phone) {
        return this.info.store_Phone.replace(/(.{3}).*(.{4})/, '$1****$2')
      }
      return ''
    },
  },

  filters: {
    dateFormat(str) {
      return dayjs(str).form
    },
  },

  onLoad(e) {
    this.id = e.id
  },

  onShow() {
    this.getInfo(this.id)
  },

  methods: {
    dateFormat,
    dayDiff,

    //获取订单信息
    getInfo(id) {
      if (this.pageLoading) {
        uni.showLoading()
      }

      this.$api.accessory
        .getInquiryInfo({
          id: id,
        })
        .then((res) => {
          this.info = res.data
          if (this.info.quote_Status === 10) {
            this.remainder = this.dayDiff(this.info.effective_Time)
          } else if (this.info.quote_Status === 30) {
            this.getResetDate()
          }

          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
        .catch((e) => {
          console.log(e)
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
    },

    //取消询价
    cancle() {
      this.showModal = true
    },

    //分享询价
    share() {},

    //编辑
    edit() {
      uni.navigateTo({
        url: '/pages_findAccessory/newInquiry/newInquiry?type=1&id=' + this.id,
      })
    },

    //图片处理
    handdleImg(imgStr) {
      if (imgStr) {
        return JSON.parse(imgStr)
      }
    },

    //弹窗显示处理
    handleModal(type) {
      this.eventType = type
      if (type === 'del') {
        this.modalText = '删除询价后将无法恢复，是否确认删除?'
        this.modalBtnLeft = '我再想想'
        this.modalBtnRight = '确定删除'
        this.modalTitle = '删除询价'
      } else if (type === 'cancel') {
        this.modalText = '取消询价后，商家无法看到您的询件寻求，是否确认取消?'
        this.modalBtnLeft = '我再想想'
        this.modalBtnRight = '确定取消'
        this.modalTitle = '取消询价'
      } else if (type === 'reset') {
        this.modalText =
          '上架此询价信息后，有效期截止 为' +
          this.resetDate +
          '日，是否确认上架?'
        this.modalBtnLeft = '取消'
        this.modalBtnRight = '确定上架'
        this.modalTitle = '上架'
      }
      this.showModal = true
    },

    //取消
    modalCancel() {
      this.showModal = false
    },

    //确认
    async modalConfirme() {
      try {
        let res = ''
        if (this.eventType === 'del') {
          res = await this.$api.accessory.deleteCarPart({
            id: this.id,
          })
        } else if (this.eventType === 'cancel') {
          res = await this.$api.accessory.cancelCarPart({
            id: this.id,
            quote_Status: 0,
          })
        } else if (this.eventType === 'reset') {
          res = await this.$api.accessory.onTheShelfCarPart({
            id: this.id,
          })
        }
        uni.showToast({
          title: '提交成功',
          icon: 'none',
          duration: 3000,
        })
        this.showModal = false
        setTimeout(() => {
          uni.navigateBack({
            delta: 1,
          })
        }, 3000)
      } catch (e) {
        console.log(e)
        this.showModal = false
      }
    },

    //复制
    copy(id) {
      // #ifndef H5
      uni.setClipboardData({
        data: id,
        success: function (res) {
          console.log(res)
          uni.showToast({
            title: '复制成功',
            icon: 'none',
          })
        },
      })
      // #endif
    },

    //重新上架有效期
    getResetDate() {
      const now = new Date().getTime() + 7 * 86400000
      this.resetDate = this.dateFormat(now)
    },
  },
}
